/**************************************************************************************************
 * @file        os_memery_manager.h
 * @brief       跨平台内存管理器封装，基于TLSF算法实现
 * @details     提供统一的内存管理接口，支持内存泄漏检测和调试功能
 *
 * @author      taoist.luo
 * @version     1.0.0
 * @date        2024-09
 *
 * Copyright (c) 2024 taoist.luo (https://gitee.com/harmonyluo(https://blog.csdn.net/harmonyluo))
 *
 * MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *************************************************************************************************/
#pragma once

#include <atomic>
#include <cstddef>
#include <mutex>
// 内存块头部信息
constexpr uint64_t MEMORY_MAGIC = 0x4D454D4F52592A2B;
struct memory_header_t {
    uint64_t magic;
    size_t size;
    const char* file;
    int line;
    memory_header_t* next;
    memory_header_t* prev;
};

class os_memery_manager_t
{
public:
    static os_memery_manager_t* get_instance();
    static void destroy();
    void init();

    void* allocate(size_t size, const char* file, int line);
    void deallocate(void* ptr);

    void print_leak_report();

    // 当前内存使用量(已占用)
    size_t get_current_allocated() const;
    // 内存分配峰值（最高分配到多少）
    size_t get_peak_allocated() const;
    // 总内存分配（总分配了多少次）
    size_t get_total_allocated() const;
    // 总内存释放量（总共释放了多少次）
    size_t get_total_deallocated() const;
    // 总内存池总量
    size_t get_total_memory() const;
    // 剩余内存量（当前内存池剩余多少）
    size_t get_remaining_memory() const;

private:
    os_memery_manager_t();
    ~os_memery_manager_t();

private:
    static std::atomic<os_memery_manager_t*> m_instance;
    static std::mutex m_mutex;

    void* m_pool;
    size_t m_pool_size;
    memory_header_t* m_allocated_blocks;
    size_t m_total_allocated;
    size_t m_total_deallocated;
    size_t m_current_allocated;
    size_t m_peak_allocated;
};

// 获取内存管理器实例
os_memery_manager_t* get_memery_manager();
// 打印内存泄漏报告
void print_memory_leak_report();
// 程序启动时自动初始化 TLSF
// 声明函数
// void init_tlsf_allocator();
void cleanup_tlsf_allocator();
// asm volatile("" : : "r"((void*)&init_tlsf_allocator));
#define os_init                                                   \
    {                                                             \
        asm volatile("" : : "r"((void*)&cleanup_tlsf_allocator)); \
    }

